using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using Microsoft.ConnectionEngine.Common;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using System.Reflection;
using System.IO;
using System.Web;

namespace Microsoft.ConnectionEngine.Common
{
    /// <summary>
    /// Provides a set of utility methods for common tasks
    /// </summary>
    public class Utilities
    {
        /// <summary>
        /// Provide the details of the destination Provider
        /// </summary>
        /// <param name="destinationproviderid"></param>
        /// <param name="providerList"></param>
        /// <returns></returns>
        [Obsolete("Should be using the code within SystemAdapterBase")]
        public static ServiceProvider GetDestinationProvider(string destinationproviderid, ServiceProviderList providerList)
        {
            foreach (ServiceProvider provider in providerList.PoolList[0].ServiceProviders)
            {
                if (provider.ServiceProviderID.Equals(destinationproviderid))
                    return provider;
            }
            return null;
        }

        /// <summary>
        /// Serializes an object based on its type.
        /// </summary>
        /// <param name="content">The object to be serialized.</param>
        /// <param name="contentType">The type of the object being serialized.</param>
        /// <returns>A string representing the serialized object.</returns>
        public static string SerializeObject(object content, Type contentType)
        {
            System.Text.StringBuilder doc = new System.Text.StringBuilder();

            XmlSerializer serializer = new XmlSerializer(contentType);
            System.IO.TextWriter tw = new System.IO.StringWriter(doc);
            serializer.Serialize(tw, content);
            tw.Close();

            return doc.ToString();
        }

        /// <summary>
        /// Get an instance of an object based on a serialized string and its type.
        /// </summary>
        /// <param name="content">A string representing the serialized object.</param>
        /// <param name="contentType">The type of the object being deserialized.</param>
        /// <returns>An object representation of the object.</returns>
        public static object DeserializeObject(string content, Type contentType)
        {
            XmlSerializer serializer = new XmlSerializer(contentType);
            System.IO.TextReader tr = new System.IO.StringReader(content);
            return serializer.Deserialize(tr);
        }

        /// <summary>
        /// If int is null return min value
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ConvertNullableInt(string value)
        {
            if (string.IsNullOrEmpty(value))
                return int.MinValue;

            //Work Item ID: 1603
            //Modified By; SachinJo
            //if (value.Trim() == "")
            if (value.Trim().Length == 0) 
                return int.MinValue;
            else
                return Convert.ToInt32(value);
        }

        /// <summary>
        /// Convert a string to its equivalent enumeration representation from within an enumeration type passed as parameter.
        /// </summary>
        /// <param name="t">The enumeration type being parsed.</param>
        /// <param name="Value">The string representing one of the enumeration values.</param>
        /// <returns>Returns a object representation of the enumeration instance that matches the value.</returns>
        public static object StringToEnum(Type t, string Value)
        {
            foreach (FieldInfo fi in t.GetFields())
                if (fi.Name.Equals(Value))
                    return fi.GetValue(null);    // We use null because
            // enumeration values
            // are static

            throw new Exception(string.Format("Can't convert {0} to {1}", Value, t.ToString()));
        }

        /// <summary>
        /// Returns the physical file path that corresponds to the specified relative path for the current
        /// Web server virtual path or Application current path.
        /// </summary>
        /// <param name="path">The relative path.</param>
        /// <returns>The physical file path that corresponds to the relative path.</returns>
        public static string MapPath(string path)
        {
            //Check that the given path is a relative path. If not then don't change it
            if (!Path.IsPathRooted(path))
            {
                string root = string.Empty;

                //We make the assumption that if the HttpContext.Current is not null then
                //  the application is running in the web environment
                if (HttpContext.Current != null)
                {
                    root = HttpContext.Current.Server.MapPath("");
                }
                else
                {
                    root = AppDomain.CurrentDomain.BaseDirectory;
                }

                path = Path.Combine(root, path);
            }

            return path;
        }
    }
}
